import com.powerfl.frm.taf.*;

import flash.utils.Dictionary;

private var _tag:Object;

private function tagInit():void{
	_tag = {};
}
final public function addTags( $tree:String, ...$tags ):void{
	orderAdd( null, function():void{
		addTags_( $tree, $tags );
	} );
}
private function addTags_( $tree:String, $tags:Array ):void{
	var tag:*, key1:*, key2:*;
	for each( key1 in $tags ){
		if( key1 is TAFparam ){
			if( key1.tag.length ){
				for each( key2 in key1.tag ){
					addTag_( $tree, key2 );
				}
			}
		}else if( key1 ){
			addTag_( $tree, key1 );
		}
	}
}
private function addTag_( $tree:String, $tag:String ):void{
	if( SELF._tag[$tag] ){}else{
		SELF._tag[$tag] = [];
	}
	if( SELF._tag[$tag].indexOf( $tree ) < 0 ){
		SELF._tag[$tag][SELF._tag[$tag].length] = $tree;
	}
}
final public function tagRemove( $tree:String, $tag:String ):void{
	orderAdd( null, function():void{
		var i:int;
		i = SELF._tag[$tag].indexOf( $tree );
		if( i > -1 ){
			SELF._tag[$tag].splice( i, 1 );
		}
	} );
}
final public function tagAllRemove():void{
	orderAdd( null, function():void{
		SELF._tag = {};
	} );
}
final public function tagTreeRemove( ...$trees ):void{
	orderAdd( null, function():void{
		tagTreeRemove_( $trees );
	} );
}
private function tagTreeRemove_( $trees:* ):void{
	var i:int, tree:*, key:*;
	if( $trees is Array ){
		for each( tree in $trees ){
			for( key in SELF._tag ){
				i = SELF._tag[key].indexOf( tree );
				if( i > -1 ){
					SELF._tag[key].splice( i, 1 );
				}
			}
		}
	}else{
		for( key in SELF._tag ){
			i = SELF._tag[key].indexOf( tree );
			if( i > -1 ){
				SELF._tag[key].splice( i, 1 );
			}
		}
	}
}
final public function tagTagRemove( ...$tags ):void{
	orderAdd( null, function():void{
		var tag:*
		for each( tag in $tags ){
			if( SELF._tag[tag] ){
				SELF._tag[tag].length = 0;
			}
		}
	} );
}
final public function tagGet( ...$tags ):Array{
	var result:Array, key:*;
	result = [];
	for each( key in $tags ){
		if( SELF._tag[key] ){
			result = result.concat( SELF._tag[key] );
		}
	}
	return result;
}